/*
 * SystemTimer.c
 *
 * Created: 4/1/2013 6:54:32 PM
 *  Author: Administrator
 */ 

#include <avr/io.h>
#include <util/delay.h>
#include <avr/interrupt.h>

#include "SystemTimer.h"
#include "System.h"
#include "Pins.h"
#include "Buttons.h"

//-------------------------------------------------------------------------------------------
// System Timer Module
//
//	This code keeps track of "real" time using Timer/Counter 2 configured to run using
//	CTC mode. The system time will then be used for
//		1) Delay calculations (for long waits)
//		2) Long term timing calculations
//-------------------------------------------------------------------------------------------

const uint8_t	SlipAt = 8;		// number of ticks to slip after

static uint16_t	SlipCount=0;	// Counts number of cycles to slip
static uint8_t		MsTicks=0;		// Rolling tick counter


static uint16_t	Milliseconds =0;	// Counts Milliseconds (0 to 1000)
static uint8_t		Seconds=0;			// counts seconds (0 to 59)
static uint8_t		Minutes =0;
static bool Elapsed = false;



//-------------------------------------------------------------------------------------------
// This call initializes the system timer to fire at the correct interval
// This should ONLY be called at system startup and then never again!
//
//	InputArguments: None
//	OutputArguments: None
//-------------------------------------------------------------------------------------------
void InitializeSystemTimer(void)
{
//-------------------------------------------------------------------------
// Initialize Timer2 in CTC mode
// See datasheet Section 14.7.2
//
//	The resulting period will be
//
//		T   =       N * (1 + OCR2A)
//				 ----------------------
//						 Fclk
//
//		Using N = 256 and OCR2A =77
//
//
//		We get a period of 19968/ 20 000 000
//
//		To get closer to the desired 1ms period we drop a millisecond count
//		every 2496 ticks which puts the count much closer to where we want
//
//-------------------------------------------------------------------------


	TCCR2A = (1 << WGM21);	// mode = CTC (Rolls over at OCR2A) OC2A disconnected.
	OCR2A = 77;
 
	TCCR2B = ( 1 << CS21) | ( 1 << CS22);
	
	TIMSK2 =(1 << OCIE2A);	// Interrupt on 	OCRA Match

//-------------------------------------------------------------------------
// Reset the timer variables
//-------------------------------------------------------------------------
	

	timer_reset();

}








//------------------------------------------------------------------------------------------------
// System timer interrupt handler
//------------------------------------------------------------------------------------------------
ISR(TIMER2_COMPA_vect) 
{
	
//-------------------------------------------------------------
// 	Increment the free-running system tick counter which is
//	used for waits
//-------------------------------------------------------------
	if (MsTicks >0)
		MsTicks--;
		
	// Increment the milliseconds timer	
	SlipCount++;
	if (SlipCount >=2496) 
	{
		SlipCount=0;
	} else {
		
		poll_buttons_ISR();		
		Milliseconds++;
		if (Milliseconds >=1000)
		{
			Milliseconds=0;
			Seconds++;
			if (Seconds>=60)
			{
				Minutes++;
				Seconds=0;
			}
			
			Elapsed=true;
		}
	}

}


uint8_t timer_reset(void)
{
	Milliseconds=0;
	Elapsed=false;
	Seconds=0;
	Minutes=0;	
}


uint8_t timer_get_seconds(void)
{
	return Seconds;
}

uint8_t timer_get_minutes(void)
{
	return Minutes;
	
}

bool has_second_elapsed(void)
{
	if (Elapsed)
	{
		Elapsed = false;
		return true;		
	}
	return false;	
	
	
}










